package com.tanhua.server.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * [视频] 模块
 */
@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private MovementsController movementsController;
    @Autowired
    private CommentsController commentsController;
    @Autowired
    private VideoMQService videoMQService;

    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        String id = this.videoService.saveVideo(picFile, videoFile);
        if (StringUtils.isNotEmpty(id)) {
            //发送消息
            this.videoMQService.videoMsg(id);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 查询小视频列表
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        //前端APP携带的页码是0开始，在VideoApiImpl中已经都对page进行了减1操作，所有这里将page置为1
        if (page == 0) page = 1;
        PageResult pageResult = this.videoService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{videoId}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("videoId") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.likeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息
            this.videoMQService.likeVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 取消点赞
     */
    @PostMapping("/{videoId}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("videoId") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.disLikeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息
            this.videoMQService.disLikeVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 评论列表
     */
    @GetMapping("/{videoId}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("videoId") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        return this.commentsController.queryCommentsList(videoId, page, pagesize);
    }

    /**
     * 提交评论
     */
    @PostMapping("/{videoId}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("videoId") String videoId) {
        param.put("movementId", videoId);
        ResponseEntity<Void> entity = this.commentsController.saveComments(param);
        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息
            this.videoMQService.commentVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 评论点赞
     */
    @PostMapping("/comments/{publishId}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("publishId") String publishId) {
        return this.movementsController.likeComment(publishId);
    }

    /**
     * 评论取消点赞
     */
    @PostMapping("/comments/{publishId}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("publishId") String publishId) {
        return this.movementsController.disLikeComment(publishId);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{userId}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("userId") Long userId) {
        Boolean bool = this.videoService.followUser(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消视频用户关注
     */
    @PostMapping("/{userId}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("userId") Long userId) {
        Boolean bool = this.videoService.disFollowUser(userId);
        return ResponseEntity.ok(null);
    }
}
